<?php

class Pagination extends Model{
  
	public $results;
	public $pageSize;
	public $page;
	public $row;
	public $resultPageName;
	public $pageLinks;
	public $totalRows;

	public $resultSet  = false;

	/*Full constructor
	* @param 	$query - mySQL query to be displayed
	* @param 	$pageSize - max number of rows to be displayed
	* @param 	$cnx - active mySQL connection
	* @param 	$resultPageName - HTTP REQUEST variable name
						REQUIRED to support multiple instances of Pagination in one PHP page
						should be unique in each instance of Pagination in same PHP page
	* @param 	$pageLinks - number of links displyaed in getPageNav()
	*/

	public function Pagination($query="", $pageSize=10, $resultPageName="resultPage", $pageLinks=10) {
   
   		if(!$this->connection){
			$this->sql_connect();
		}

		$resultpage 		  = $_GET[$resultPageName];
		$this->resultPageName = $resultPageName;
		$this->pageLinks 	  = $pageLinks;
		$this->pageSize 	  = $pageSize;

		$this->resultSet 	 = $this->sql_query($query);
		$this->results 	 	 = $this->sql_query($query);

		if ((int)$resultpage <= 0) $resultpage = 1;
		if ($resultpage > $this->getNumPages()) $resultpage = $this->getNumPages();
		$this->setPageNum($resultpage);
	}

	public function getNumPages(){
	
		if (!$this->results) return FALSE;
		return ceil(mysql_num_rows($this->results) / (float)$this->pageSize);
	}

	public function setPageNum($pageNum){
	
		if ($pageNum > $this->getNumPages() or $pageNum <= 0) return FALSE;

		$this->page = $pageNum;
		$this->row = 0;
		mysql_data_seek($this->results,($pageNum-1) * $this->pageSize);
	}

	public function getPageNum(){
		return $this->page;
	}

	public function isLastPage(){
		return ($this->page >= $this->getNumPages());
	}

	public function isFirstPage(){
		return ($this->page <= 1);
	}

	public function fetchAssoc(){
		if (!$this->results) return FALSE;
		if ($this->row >= $this->pageSize) return FALSE;
		$this->row++;
		return mysql_fetch_assoc($this->results);
	}
  
  	public function getTotalRowCount(){
  		if (!$this->results) return FALSE;
  		return mysql_num_rows($this->results);
  	}

	public function fetchObject(){
	
		if (!$this->results) return FALSE;
		if ($this->row >= $this->pageSize) return FALSE;
		$this->row++;
		return mysql_fetch_object($this->results);
	}
  
	public function buildLinkNavigation($queryvars = ''){
    
		$nav = '';
		if (!$this->isFirstPage()){
			$nav .= "<a href=\"$queryvars&".$this->resultPageName."=".($this->getPageNum()-1)."\">Prev</a>&nbsp;";
		}
		
		if ($this->getNumPages() > 1)
		
			if ($this->getNumPages() > $this->pageLinks ) { //if the # of pageLinks is more than the max allowed

				//if last N pageLinks
				if ($this->page + $this->pageLinks > $this->getNumPages()){
					for ($i=$this->getNumPages() - $this->pageLinks; $i<=$this->getNumPages(); $i++){
						if ($i==$this->page)
							$nav .= "<b>$i </b>";
						else
							$nav .= "<a href=\"$queryvars&".$this->resultPageName."={$i}\">{$i}</a> ";
					}
				}
				else{
					$start = $this->page - ($this->pageLinks/2);
					
					// if first N pageLinks
					if ($start <= 1) {
						for ($i=1; $i <= $this->pageLinks + 1; $i++){
							if ($i==$this->page)
								$nav .= "<b>$i </b>";
							else
								$nav .= "<a href=\"$queryvars&".$this->resultPageName."={$i}\">{$i}</a> ";
						}
					}
					else { //middle
						for ($i= (int) $start; $i <= $start+$this->pageLinks; $i++){
							if ($i==$this->page)
								$nav .= "<b>$i </b>";
							else
								$nav .= "<a href=\"$queryvars&".$this->resultPageName."={$i}\">{$i}</a> ";
						}
					}
				}
			}
			else { //if the # of pageLinks is less than the max allowed
	      
				for ($i=1; $i<=$this->getNumPages(); $i++){
					if ($i==$this->page)
						$nav .= "<b>$i </b>";
					else
						$nav .= "<a href=\"$queryvars&".$this->resultPageName."={$i}\">{$i}</a> ";
				}
			}
			
		if (!$this->isLastPage()){
			$nav .= "<a href=\"$queryvars&".$this->resultPageName."=". ($this->getPageNum()+1).'">Next</a> ';
		}

		return $nav;
	}

	public function buildNavigation($url = ''){
    
    	$this->totalRows = $this->getTotalRowCount();

    	$nav = '<p>Total of <b>'.$this->totalRows.'</b> results found</p>';
		$nav .= '<p class="page">';

			//previous link
			if (!$this->isFirstPage()){
				$prevurl = $url."&".$this->resultPageName."=".($this->getPageNum()-1);
				$nav .= "<a href='".$prevurl."'>
							<span class='prev' title='Previous'>&nbsp;</span>
						</a>";
			}
			
			if ($this->getNumPages() > 1){
				$nav .= "<span>Page ";
				$nav .= "<input type='hidden' id='page_url' name='page_url' value='".$url."' />";
				$nav .= "<select name='pagelinks' id='pagelinks' class='dropdown'>";
						for($i=1; $i<=$this->getNumPages(); $i++){
							$currentPage = $this->getPageNum();
							if($i == $currentPage){
								$nav .= "<option value='".$i."' selected='selected'>".$i."</option>";
							}else{
								$nav .= "<option value='".$i."'>".$i."</option>";
							}
							
						}
				$nav .= "</select>";
				$nav .= " of ".$this->getNumPages()."</span>";

				//next link
				if (!$this->isLastPage()){
					$nexturl = $url."&".$this->resultPageName."=".($this->getPageNum()+1);
					$nav .= "<a href='".$nexturl."'>
								<span class='next' title='Next'>&nbsp;</span>
							</a>";
				}
			}

		$nav .= "</p>";
		return $nav;
	}
  
}

?>